﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using DevExpress.AgDataGrid.Data;
namespace DevExpress.AgDataGrid.Internal {
	public class Selection {
		DataController dataController;
		public Selection(DataController dataController) {
			this.dataController = dataController;
		}
		public int Count { get { return DataController.SelectionController.Count; } }
		public void SelectAll() { DataController.SelectionController.SelectAll(); }
		public void ClearSelected() { DataController.SelectionController.Clear(); }
		public bool IsSelected(int rowHandle) { return DataController.SelectionController.GetSelected(rowHandle); }
		public void Select(int rowHandle) { DataController.SelectionController.SetSelected(rowHandle, true); }
		public void UnSelect(int rowHandle) { DataController.SelectionController.SetSelected(rowHandle, false); }
		public void InvertSelection(int rowHandle) {
			if(IsSelected(rowHandle)) {
				UnSelect(rowHandle);
			} else {
				Select(rowHandle);
			}
		}
		protected internal void Select(int rowHandle, bool invert) {
			if(invert) {
				InvertSelection(rowHandle);
			} else {
				Select(rowHandle);
			}
		}
		protected internal void SelectRange(int rowHandle) {
			DataController.SelectionController.SelectRange(shiftStartRow, rowHandle);
		}
		protected DataController DataController { get { return dataController; } }
		int shiftStartRow = DataController.InvalidRow;
		protected internal void SelectRange(int rowHandle, bool ctrl, bool shift) {
			if(ctrl && shift) {
				if(shiftStartRow == DataController.InvalidRow) {
					shiftStartRow = DataController.FocusedControllerRowIndex;
				}
				SelectRange(rowHandle);
				return;
			}
			if(ctrl) {
				shiftStartRow = DataController.InvalidRow;
				InvertSelection(rowHandle);
				return;
			}
			if(shift) {
				if(shiftStartRow == DataController.InvalidRow) {
					shiftStartRow = DataController.FocusedControllerRowIndex;
				}
				ClearSelected();
				SelectRange(rowHandle);
				return;
			}
			shiftStartRow = DataController.InvalidRow;
			ClearSelected();
			Select(rowHandle);
		}
	}
}
